 package com.withoutwords.engine.social;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.withoutwords.R;
import com.withoutwords.engine.Game;
import com.withoutwords.engine.GameController;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

public class SocialEngine {
	//for singleton implementation
	private SocialEngine()
	{
		loginNetowrk = SocialNetwork.Facebook;
	}
	static SocialEngine _engine;
	public static SocialEngine getSocialEngine()
	{
		if(_engine == null)
		{
			_engine = new SocialEngine();
			
		}
		return _engine;
	}
	
	//Class members and functions
	SocialNetwork loginNetowrk;
	ISocialNetworkController[] socialNetworksControllers = new ISocialNetworkController[SocialNetwork.values().length];
	public ISocialNetworkController getSocialNetworkController(SocialNetwork network)
	{
		int index  = network.ordinal();
		if(socialNetworksControllers[index] == null)
		{
			socialNetworksControllers[index] = createSocialNetworkController(network);
		}
		return socialNetworksControllers[index];
	}
	 ISocialNetworkController createSocialNetworkController(SocialNetwork network)
	{
		switch(network)
		{
		case Facebook:
			return new FacebookController();
		}
		
		return null;
	}
	
	 public boolean isLoggenIn(Activity activity)
	 {
		 boolean isLoggedIn = false;
		 ISocialNetworkController loginNetworkController = getSocialNetworkController(loginNetowrk);
		 if(loginNetworkController != null)
		 {
			 isLoggedIn = loginNetworkController.isLoggenIn( activity);
		 }
		 return isLoggedIn;
	 }
	 
	 public void logout(Context contex)
	 {
		 ISocialNetworkController loginNetworkController = getSocialNetworkController(loginNetowrk);
		 if(loginNetworkController != null)
		 {
			 loginNetworkController.logout( contex);
		 }
		 
	 }
	 public void shareDrawing(final Context context,final String sharedata,byte[] bdata)
		{ 
			//Bitmap icon = mBitmap;
			Intent share = new Intent(Intent.ACTION_SEND);
			share.setType("image/jpeg");
			//ByteArrayOutputStream bytes = new ByteArrayOutputStream();
			//icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
			String FilePath = Environment.getExternalStorageDirectory() + File.separator + "drawing.jpg";
			File f = new File(FilePath);
			try {
			    f.createNewFile();
			    FileOutputStream fo = new FileOutputStream(f);
			    fo.write(bdata);
			} catch (IOException e) {                       
			        e.printStackTrace();
			}
			String ownerName = context.getResources().getString(R.string.friend);
			Game currentgame  = GameController.getInstance().getCurrentGame();
			if(currentgame != null)
			{
			SocialUser owner = currentgame.getGameOwner();
			ownerName = owner.Name;
			}
			
			share.putExtra(Intent.EXTRA_TEXT,context.getResources().getString(R.string.share_game_text) +" " + ownerName);
			share.putExtra(Intent.EXTRA_SUBJECT, context.getResources().getString(R.string.app_name) );
			share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
			context.startActivity(Intent.createChooser(share, context.getResources().getString(R.string.share_game_title)));
			
		}

	 
}
